<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\RequestLog;

class StatsController extends Controller
{
    public function requests()
    {
        // Loading data from last 30 days.
        return \DB::select('SELECT * FROM
                (
                    SELECT
                        COUNT(id) AS requests,
                        SUM(size) AS traffic,
                        CAST(created_at AS DATE) AS date
                    FROM request_logs
                    GROUP BY date
                    ORDER BY date DESC
                    LIMIT 30
                ) a
                ORDER BY date;');
        /*
        return RequestLog::whereDate('created_at', '>', Carbon::now()->subDays(30))
            ->orderBy('created_at')

            ->get()
            ->groupBy(function ($val) {
                return $val->created_at->format('md');
            })
            ->map(function ($val) {
                return ['requests' => count($val), 'traffic' => collect($val)->sum('size')];
            });
        */
    }
}
